package com.ruoyi.admin.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.admin.domain.SysUserDto;
import com.ruoyi.admin.service.IMlGoodsDtoService;
import com.ruoyi.admin.service.ISysUserDtoService;
import com.ruoyi.admin.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.admin.domain.MlOrderDto;
import com.ruoyi.admin.service.IMlOrderDtoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/admin/order")
public class MlOrderDtoController extends BaseController
{
    @Autowired
    private IMlOrderDtoService mlOrderDtoService;
    @Autowired
    private ISysUserDtoService sysUserDtoService;
    /**
     * 1.传入userId查询用户表获得用户名称和用户地址,将他们存到Map中返回到前端
     * 2.传入userId查询商品表获得商品信息,将他们存到Map中返回到前端
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @RequestMapping("/selectUsernameAndAddressByUserId/{userId}")
    public Result selectUsernameAndAddressByUserId(@PathVariable Long userId){
        Map<String,Object> result1=sysUserDtoService.selectUsernameAndAddressByUserId(userId);
        Map<String,Object> result2=mlOrderDtoService.selectGoodsInfoWhereUserId(userId);
        return Result.success().put("UsernameAndAddressByUserId",result1).put("selectGoodsInfoWhereUserId",result2);
    }


    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MlOrderDto mlOrderDto)
    {
        startPage();
        List<MlOrderDto> list = mlOrderDtoService.selectMlOrderDtoList(mlOrderDto);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('admin:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MlOrderDto mlOrderDto)
    {
        List<MlOrderDto> list = mlOrderDtoService.selectMlOrderDtoList(mlOrderDto);
        ExcelUtil<MlOrderDto> util = new ExcelUtil<MlOrderDto>(MlOrderDto.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mlOrderDtoService.selectMlOrderDtoById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MlOrderDto mlOrderDto)
    {
        return toAjax(mlOrderDtoService.insertMlOrderDto(mlOrderDto));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MlOrderDto mlOrderDto)
    {
        return toAjax(mlOrderDtoService.updateMlOrderDto(mlOrderDto));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('admin:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mlOrderDtoService.deleteMlOrderDtoByIds(ids));
    }
}
